package org.keyroy.mapEditor.ui.beans;

import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;
import org.keyroy.mapEditor.ui.dialogs.TextInputDialog;

public class UIUtil {

	public static final Object TREE_NODE_MAIN = 0;
	public static final Object TREE_NODE_SUB = 1;

	public static void center(Shell shell) {
		Rectangle displayBounds = shell.getDisplay().getPrimaryMonitor().getBounds();
		Rectangle shellBounds = shell.getBounds();
		int x = displayBounds.x + (displayBounds.width - shellBounds.width) >> 1;
		int y = displayBounds.y + (displayBounds.height - shellBounds.height) >> 1;
		shell.setLocation(x, y);
	}

	public static void open(Shell shell) {
		center(shell);
		shell.open();
	}

	public static void getInputName(Shell shell, String text, TextInputDialog.OnClickListener clickListener) {
		TextInputDialog textInputDialog = new TextInputDialog(shell, clickListener);
		textInputDialog.setText(text);
		UIUtil.center(textInputDialog);
		textInputDialog.open();
	}

	public static Image getImage(String path) {
		return SWTResourceManager.getImage(UIUtil.class, path);
	}

	public static void removeAll(CTabFolder cTabFolder) {
		CTabItem[] cTabItems = cTabFolder.getItems();
		for (int i = 0; i < cTabItems.length; i++) {
			cTabItems[i].dispose();
		}
	}
	
	public static void removeAll(Composite composite) {
		Control[] controls = composite.getChildren();
		for (int i = 0; i < controls.length; i++) {
			controls[i].dispose();
		}
		composite.layout();
		composite.redraw();
	}
	
	
}
